package com.huabo.system.service.impl;

import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblMonitorSolutionresultMySql;
import com.huabo.system.mysql.mapper.TblMonitorSolutionresultMySqlMapper;
import com.huabo.system.oracle.entity.TblMonitorSolutionresult;
import com.huabo.system.oracle.mapper.TblMonitorSolutionresultMapper;
import com.huabo.system.service.TblMonitorSolutionresultService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TblMonitorSolutionresultServiceImpl implements TblMonitorSolutionresultService {

    @Resource
    private TblMonitorSolutionresultMapper tblMonitorSolutionresultMapper;

    @Resource
    private TblMonitorSolutionresultMySqlMapper tblMonitorSolutionresultMySqlMapper;


    @Override
    public List<String> find(String table, BigDecimal SoultionId, String ruleid, String zt) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblMonitorSolutionresultMapper.findRuleidAndAcctid(SoultionId, ruleid);
        } else {
            return tblMonitorSolutionresultMySqlMapper.findRuleidAndAcctid(SoultionId, ruleid);
        }
    }

    @Override
    public Map<String, Object> findBySoultionIdZKZX(String table, BigDecimal SoultionId, String rulid, String zt, PageInfo<TblMonitorSolutionresult> pageInfo) {
//        String sql = "select *  from \"" + zt + "\"." + table + " where  EXECTIME = (SELECT SIGNID FROM TBL_MONITOR_PREWARNING WHERE SOLUTIONRESULTID = ( select SOLUTIONRESULTID from ( SELECT SOLUTIONRESULTID FROM TBL_MONITOR_SOLUTIONRESULT  WHERE SOURCE = 3 AND SOLUTIONID = " + SoultionId + " order by savetime desc) where rownum=1) and RULEID=" + rulid + ") ";
//        String sqlCount = "select count(*)  from \"" + zt + "\"." + table + " where  EXECTIME = (SELECT SIGNID FROM TBL_MONITOR_PREWARNING WHERE SOLUTIONRESULTID = ( select SOLUTIONRESULTID from ( SELECT SOLUTIONRESULTID  FROM TBL_MONITOR_SOLUTIONRESULT WHERE SOURCE = 3 AND SOLUTIONID = " + SoultionId + " order by savetime desc) where rownum=1) and RULEID=" + rulid + ")";

        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        pageInfo.setTlist(tblMonitorSolutionresultMapper.findBySoultionIdZKZX(zt, table, SoultionId, rulid, pageInfo));
        pageInfo.setTotalRecord(tblMonitorSolutionresultMapper.findBySoultionIdZKZXCount(zt, table, SoultionId, rulid));
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        resultMap.put("data", pageInfo);
        return resultMap;
    }

    @Override
    public Map<String, Object> findByMySqlSoultionIdZKZX(String table, BigDecimal SoultionId, String rulid, String zt, PageInfo<TblMonitorSolutionresultMySql> pageInfo) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        pageInfo.setTlist(tblMonitorSolutionresultMySqlMapper.findBySoultionIdZKZX(zt, table, SoultionId, rulid, pageInfo));
        pageInfo.setTotalRecord(tblMonitorSolutionresultMySqlMapper.findBySoultionIdZKZXCount(zt, table, SoultionId, rulid));
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        resultMap.put("data", pageInfo);
        return resultMap;
    }

    @Override
    public Map<String, Object> findBySoultionIdZK(String table, BigDecimal SoultionId, String rulid, String zt, PageInfo<TblMonitorSolutionresult> pageIn) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        pageIn.setTlist(tblMonitorSolutionresultMapper.findBySoultionIdZKZX(zt, table, SoultionId, rulid, pageIn));
        pageIn.setTotalRecord(tblMonitorSolutionresultMapper.findBySoultionIdZKZXCount(zt, table, SoultionId, rulid));
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        resultMap.put("data", pageIn);
        return resultMap;
    }

    @Override
    public Map<String, Object> findByMySqlSoultionIdZK(String table, BigDecimal SoultionId, String rulid, String zt, PageInfo<TblMonitorSolutionresultMySql> pageIn) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        pageIn.setTlist(tblMonitorSolutionresultMySqlMapper.findBySoultionIdZKZX(zt, table, SoultionId, rulid, pageIn));
        pageIn.setTotalRecord(tblMonitorSolutionresultMySqlMapper.findBySoultionIdZKZXCount(zt, table, SoultionId, rulid));
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        resultMap.put("data", pageIn);
        return resultMap;
    }


}
